package com.jfinal.weixin.api;

import com.alibaba.fastjson.JSON;
import com.jfinal.utils.http.HttpUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户标签管理的相关接口,
 * 实现对公众号的标签进行创建、查询、修改、删除等操作，
 * 也可以对用户进行打标签、取消标签等操作。
 *
 * Created by shitiangao on 16/5/19.
 */
public enum UserTagApi {
    ice;

    /**
     * 创建标签
     * @param accessToken
     * @param tagName
     * @return
     */
    public ApiResult createTag(String accessToken, String tagName) {
        String url = APIConfig.WX__USER_TAGS_CREATE;
        url = url.replace("ACCESS_TOKEN", accessToken);

        Map<String, Map<String, String>> postData = new HashMap<>();
        Map<String, String> tagData = new HashMap<>();
        tagData.put("name", tagName);

        postData.put("tag", tagData);

        return ApiResult.create(HttpUtil.post(url, JSON.toJSONString(postData)));
    }

    /**
     * 编辑标签,通过 id 和 name
     * @param accessToken
     * @param tagId
     * @param tagName
     * @return
     */
    public ApiResult editTag(String accessToken, Integer tagId, String tagName) {
        String url = APIConfig.WX__USER_TAGS_EDIT;
        url = url.replace("ACCESS_TOKEN", accessToken);

        Map<String, Map<String, Object>> postData = new HashMap<>();
        Map<String, Object> tagData = new HashMap<>();
        tagData.put("name", tagName);
        tagData.put("id", tagId);

        postData.put("tag", tagData);

        return ApiResult.create(HttpUtil.post(url, JSON.toJSONString(postData)));
    }

    /**
     * 获取公众号已创建的标签
     * @param accessToken
     * @return
     */
    public ApiResult getTags(String accessToken) {
        String url = APIConfig.WX__USER_TAGS_GET;
        url = url.replace("ACCESS_TOKEN", accessToken);

        return  ApiResult.create(HttpUtil.post(url));
    }

    /**
     * 删除标签
     * 注意:当某个标签下的粉丝超过10w时，后台不可直接删除标签
     *     要删除需先进行取消标签的操作，直到粉丝数不超过10w后，才可直接删除该标签。
     * @param accessToken
     * @param tagId
     * @return
     */
    public ApiResult deleteTag(String accessToken, Integer tagId) {
        String url = APIConfig.WX__USER_TAGS_DELETE;
        url = url.replace("ACCESS_TOKEN", accessToken);

        Map<String, Map<String, Object>> postData = new HashMap<>();
        Map<String, Object> tagData = new HashMap<>();
        tagData.put("id", tagId);

        postData.put("tag", tagData);

        return ApiResult.create(HttpUtil.post(url, JSON.toJSONString(postData)));
    }

    /**
     * 得到某个标签下的粉丝列表
     * @param accessToken
     * @param tagId
     * @param nextOpenid
     * @return
     */
    public ApiResult getFansList(String accessToken, Integer tagId, String nextOpenid) {
        String url = APIConfig.WX__USER_TAGS_FANS_GET;
        url = url.replace("ACCESS_TOKEN", accessToken);

        Map<String, Object> postData = new HashMap<>();
        postData.put("tagid", tagId);
        postData.put("next_openid", nextOpenid == null ? "" : nextOpenid);

        return ApiResult.create(HttpUtil.post(url, JSON.toJSONString(postData)));
    }
    /**
     * 批量给用户打标签
     * @param accessToken
     * @param tagId
     * @param openIdList
     * @return
     */
    public ApiResult taggingBatch(String accessToken, int tagId, List<String> openIdList) {
        String url = APIConfig.WX__USER_TAGS_TAGGING_BATCH;
        url = url.replace("ACCESS_TOKEN", accessToken);

        Map<String, Object> postDataMap = new HashMap<>();
        postDataMap.put("openid_list", openIdList);
        postDataMap.put("tagid", tagId);

        return ApiResult.create(HttpUtil.post(url, JSON.toJSONString(postDataMap)));
    }

    /**
     * 批量为取消用户的标签
     * @param accessToken
     * @param tagId
     * @param openIdList
     * @return
     */
    public ApiResult untaggingBatch(String accessToken, int tagId, List<String> openIdList) {
        String url = APIConfig.WX__USER_TAGS_UNTAGGING_BATCH;
        url = url.replace("ACCESS_TOKEN", accessToken);

        Map<String, Object> postDataMap = new HashMap<>();
        postDataMap.put("openid_list", openIdList);
        postDataMap.put("tagid", tagId);

        return ApiResult.create(HttpUtil.post(url, JSON.toJSONString(postDataMap)));
    }

    /**
     * 获取用户的标签列表
     * @param accessToken
     * @param openId
     * @return
     */
    public ApiResult getTagidListOfUser(String accessToken, String openId) {
        String url = APIConfig.WX__USER_TAGS_USER_GET;
        url = url.replace("ACCESS_TOKEN", accessToken);

        Map<String, Object> postDataMap = new HashMap<>();
        postDataMap.put("openid", openId);

        return ApiResult.create(HttpUtil.post(url, JSON.toJSONString(postDataMap)));
    }

}
